package com.wn.servlet;

import com.alibaba.fastjson.JSON;
import com.wn.service.OwnerService;
import com.wn.service.impl.OwnerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *    01  url 不要写错了
 *    02  获取   key不能写错
 *          get        的数据在url上面 可以通过
 *                        String id = req.getParameter("id");
 *          post       的的数据在请求体中
 *                        通过 stream获取 这个操作 繁琐 (把前端的post的统一改成get)
 *                        后面框架中、再使用post
 *    03  乱码问题
 *          resp.setContentType("text/html;charset=utf-8");   无脑添加这个
 *
 *    04  多用elementUI 增删改查前端搞定
 *
 */
@WebServlet("/owner/batchdel")
public class OwnerDelAllServlet extends HttpServlet {

    private OwnerService ownerService = new OwnerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //回写的数据设置 编码格式
        resp.setContentType("text/html;charset=utf-8");

        //处理请求
        String[] ids = req.getParameterValues("ids");
        System.out.println("ids = " + ids);

        boolean row = ownerService.deleteOwnerByIds(ids);

        // 返回数据
        Map<String,Object> result = new HashMap<>();
        result.put("code",row ? "200" :"2000");
        result.put("msg",row ? "删除成功":"删除失败");
        //使用json框架 可以直接把java对象转成 json 字符串
        String jsonString = JSON.toJSONString(result);
        resp.getWriter().write(jsonString);
    }

}
